<template>
  <a-card :bordered="false">

    <a-col :md="3" :sm="24">
      <template>



        <a-tree
          defaultExpandAll="true"
          @select="onSelect"
          :selectedKeys="selectedKeys"
          :checkedKeys="checkedKeys"
          :treeData="departTree"
          :checkStrictly="checkStrictly"
          :expandedKeys="iExpandedKeys"
          @expand="onExpand"/>

      </template>
    </a-col>

    <a-col :md="21" :sm="24">

      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="人员">
                <j-search-select-tag   v-model="queryParam.userId"
                                       :trigger-change="true" dict="sys_user,username,id"
                                       placeholder="请选择人员"/>
              </a-form-item>
            </a-col>

            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="仓库">
                <j-select-all-cang-ku  v-model="queryParam.ckId"></j-select-all-cang-ku>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="类型">
                 <j-selec
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>

            </span>
            </a-col>



          </a-row>
        </a-form>
      </div>
      <!-- 查询区域-END -->

      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="handleAddUserRole" type="primary" icon="plus" style="margin-top: 16px">批量新增用户</a-button>
        <Select-User-Modal ref="selectUserModal" @selectFinished="selectOK"></Select-User-Modal>
        <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
        <!--      <a-button type="primary" icon="download" @click="handleExportXls('仓库和人员的权限表')">导出</a-button>-->
        <!--      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
        <!--        <a-button type="primary" icon="import">导入</a-button>-->
        <!--      </a-upload>-->
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
        </a-dropdown>
      </div>

      <!-- table区域-begin -->
      <div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        </div>

        <a-table
          ref="table"
          size="middle"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          class="j-table-force-nowrap"
          @change="handleTableChange">

          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
            <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
            <a-button
              v-else
              :ghost="true"
              type="primary"
              icon="download"
              size="small"
              @click="uploadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

        </a-table>
      </div>

    </a-col>


    <zcglAssetFixedCategorySetting-modal ref="modalForm" @ok="modalFormOk"></zcglAssetFixedCategorySetting-modal>
  </a-card>
</template>

<script>
import SelectUserModal from './modules/SelectUserModal'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import ZcglAssetFixedCategorySettingModal from './modules/ZcglAssetFixedCategorySettingModal'
import JSearchSelectTag from '../../components/dict/JSearchSelectTag'
import JSelectAllCangKu from '../../components/jeecgbiz/JSelectAllCangKu'
import { getAction } from '../../api/manage'

export default {
  name: "ZcglAssetFixedCategorySettingList",
  mixins:[JeecgListMixin, mixinDevice],
  components: {
    ZcglAssetFixedCategorySettingModal,JSearchSelectTag,JSelectAllCangKu,SelectUserModal
  },
  data () {
    return {
      ckId:"",
      departTree: [
        // {
        //   "id": "1585440309785092097",
        //   "name": "本级库房",
        //   "parentId": "0",
        //   "level": 1,
        //   "location": null,
        //   "sort": null,
        //   "izLeaf": null,
        //   "createBy": "admin",
        //   "createTime": "2022-10-27 09:16:41",
        //   "updateBy": "admin",
        //   "updateTime": "2022-10-27 09:16:41",
        //   "key": "1585440309785092097",
        //   "value": "1585440309785092097",
        //   "title": "本级库房",
        //   "children": [
        //     {
        //       "id": "1585440106449428481",
        //       "name": "办公用品",
        //       "parentId": "1585440309785092097",
        //       "level": 2,
        //       "location": null,
        //       "sort": null,
        //       "izLeaf": null,
        //       "createBy": "admin",
        //       "createTime": "2023-11-22 09:51:37",
        //       "updateBy": "admin",
        //       "updateTime": "2023-11-22 09:51:37",
        //       "key": "1585440106449428481",
        //       "value": "1585440106449428481",
        //       "title": "办公用品",
        //       "children": [],
        //       "leaf": false
        //     },
        //     {
        //       "id": "1585440063688499202",
        //       "name": "易耗品",
        //       "parentId": "1585440309785092097",
        //       "level": 2,
        //       "location": null,
        //       "sort": null,
        //       "izLeaf": null,
        //       "createBy": "admin",
        //       "createTime": "2023-11-22 09:51:44",
        //       "updateBy": "admin",
        //       "updateTime": "2023-11-22 09:51:44",
        //       "key": "1585440063688499202",
        //       "value": "1585440063688499202",
        //       "title": "易耗品",
        //       "children": [],
        //       "leaf": false
        //     },
        //     {
        //       "id": "1585440165014495234",
        //       "name": "存货物资",
        //       "parentId": "1585440309785092097",
        //       "level": 2,
        //       "location": null,
        //       "sort": null,
        //       "izLeaf": null,
        //       "createBy": "admin",
        //       "createTime": "2023-11-22 09:51:49",
        //       "updateBy": "admin",
        //       "updateTime": "2023-11-22 09:51:49",
        //       "key": "1585440165014495234",
        //       "value": "1585440165014495234",
        //       "title": "存货物资",
        //       "children": [],
        //       "leaf": false
        //     }
        //   ],
        //   "leaf": false
        // }
      ],
      description: '仓库和人员的权限表管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        // {
        //   title:'仓库id',
        //   align:"center",
        //   dataIndex: 'ckId'
        // },
        {
          title:'人员',
          align:"center",
          dataIndex: 'userId_dictText'
        },
        {
          title:'仓库',
          align:"center",
          dataIndex: 'ckId_dictText'
        },
        {
          title:'类型',
          align:"center",
          dataIndex: 'type',
          customRender:function (t,r,index) {
            if(t==1){
              return  "物资入库"
            }else{
              return  "物资领用"
            }

          }
        },

        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          // fixed:"right",
          width:147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/zcgl/zcglAssetFixedCategorySetting/list",
        delete: "/zcgl/zcglAssetFixedCategorySetting/delete",
        deleteBatch: "/zcgl/zcglAssetFixedCategorySetting/deleteBatch",
        exportXlsUrl: "/zcgl/zcglAssetFixedCategorySetting/exportXls",
        importExcelUrl: "zcgl/zcglAssetFixedCategorySetting/importExcel",
      },
      dictOptions:{},
      currSelected: {},
      dropTrigger: '',
      selectedKeys: [],
      checkedKeys: [],
      checkStrictly: true,
      iExpandedKeys: [],
      autoExpandParent: true,
      rightClickSelectedKey: '',
      rightClickSelectedModel: '',
      categoryModel: {},
      loading: false,
    }
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  created () {

    // this.searchQuery()
    this.loadTree()
  },
  methods: {

    selectOK(value){
      var that = this
      that.treeData = []
      that.departTree = []
      getAction('/zcgl/zcglAssetFixedCategorySetting/mulAdd?id='+this.ckId+"&ids="+value.join(",")).then((res) => {
        if (res.success) {

          this.loadData()
          this.loadTree()

        }
      })
    },

    handleAddUserRole () {
      if (this.ckId == '') {
        this.$message.error('请选择一个仓库!')
      } else {
        this.$refs.selectUserModal.visible = true
      }
    },

    onExpand (expandedKeys) {
      console.log('onExpand', expandedKeys)
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.iExpandedKeys = expandedKeys
      this.autoExpandParent = false
    },
    onSelect (selectedKeys, e) {
      this.hiding = false
      let record = e.node.dataRef
      this.currSelected = Object.assign({}, record)
      this.categoryModel = this.currSelected
      this.selectedKeys = [record.key]
      this.queryParam.ckId =  this.selectedKeys[0]
      this.ckId =  this.selectedKeys[0]
      if (record.name == '本级库房') {

        this.queryParam.ckId = ""
      }

      this.categoryModel.parentId = record.parentId


      this.loadData()
    },
    loadTree () {
      var that = this
      that.treeData = []
      that.departTree = []
      getAction('/zcgl/assetFixedCategory/queryAllCkTreeList').then((res) => {
        if (res.success) {
          //部门全选后，再添加部门，选中数量增多
          this.allTreeKeys = []
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i]
            that.treeData.push(temp)
            // if (temp.orgCategory == 0){
            if (!temp.parentId || temp.parentId == 0) {
              temp.disableCheckbox = true
              temp.selectable = true
              // temp.disabled = true;
            }
            that.departTree.push(temp)
            that.setThisExpandedKeys(temp)
          }
          this.loading = false
        }
      })
    },
    initDictConfig(){
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>